package com.wei.czz.common.dto.common;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.wei.czz.common.tool.BaseEntity;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-11-30 15:24:43
 * className: ExportExcelDto 文件导出基础类
 * version: 1.0
 * description:
 */
@Getter
@Setter
public class ExportExcelDto extends BaseEntity {

    /**
     * 序号
     */
    @ExcelProperty(value = "序号", index = 0)
    protected Integer serial;

    /**
     * 获取导出文件标题列表
     * @return 列名称列表
     */
    public List<NameValue<String>> getExportFieldList() {
        List<NameValue<String>> list = new ArrayList<>();
        // 调用类class对象
        Class<? extends ExportExcelDto> aClass = this.getClass();
        // 调用类父类class对象
        Class<?> superclass = aClass.getSuperclass();

        Field[] fields = superclass.getDeclaredFields();
        for (Field field : fields) {
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            int valueLength;
            if (Objects.isNull(annotation) || (valueLength = annotation.value().length) == 0) {
                continue;
            }
            list.add(new NameValue<>(field.getName(), annotation.value()[valueLength - 1]));
        }
        fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            int valueLength;
            if (Objects.isNull(annotation) || (valueLength = annotation.value().length) == 0) {
                continue;
            }
            list.add(new NameValue<>(field.getName(), annotation.value()[valueLength - 1]));
        }
        return list;
    }

}
